package org.ran.demo.http.end;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 第一个byte固定为长度
 * @author caoran
 *
 */
public class EndWithFixLength {
    private static final String MSG="hello world";
    public static void main(String[] args) throws Exception{
        new Thread(new Server()).start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(new Client()).start();
    }
    
    private static class Server implements Runnable {

        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket();
                serverSocket.bind(new InetSocketAddress("127.0.0.1", 8080));
                Socket socket = serverSocket.accept();
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                List<Byte> bytes = new ArrayList<>();
                int length = readLength(in);
                int b;
                int counter = 0;
                while ((b=in.read())>-1) {
                    bytes.add((byte)b);
                    counter++;
                    if (counter==length) {
                        break;
                    }
                }
                byte[] res = new byte[bytes.size()];
                for (int i = 0; i < bytes.size(); i++) {
                    res[i] = bytes.get(i);
                }
                String text = new String(res, StandardCharsets.ISO_8859_1);
                System.out.println("服务器收到请求："+text);
                out.write((byte)text.length());
                out.write(res);
                socket.close();
                serverSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    private static class Client implements Runnable{

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("127.0.0.1", 8080));
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                String request = MSG;
                out.write((byte)request.length());
                out.write(request.getBytes(StandardCharsets.ISO_8859_1));
                List<Byte> bytes = new ArrayList<>();
                int length = readLength(in);
                int b;
                int counter = 0;
                while ((b=in.read())>-1) {
                    bytes.add((byte)b);
                    counter++;
                    if (counter==length) {
                        break;
                    }
                }
                byte[] res = new byte[bytes.size()];
                for (int i = 0; i < bytes.size(); i++) {
                    res[i] = bytes.get(i);
                }
                String text = new String(res, StandardCharsets.ISO_8859_1);
                System.out.println("客户端收到响应："+text);
                socket.close();
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    private static int readLength(InputStream in) throws Exception{
        return in.read();
    }
}
